package com.sn.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sn.core.util.SnsMessageUtil;
import com.sn.core.util.pub.exception.DBExceptionEnums;
import com.sn.core.util.pub.exception.ServiceException;
import com.sn.dao.OrderMapper;
import com.sn.dao.ScoreMapper;
import com.sn.dao.UserMapper;
import com.sn.model.*;
import com.sn.service.GiftService;
import com.sn.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by qingshengzheng on 18/6/21.
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ScoreMapper scoreMapper;

    @Autowired
    private GiftService giftService;
    @Transactional
    public void addOrder(ChargeOrder order) {
        orderMapper.addChargeOrder(order);
        //增加当前用户积分
        CustomerScore score = new CustomerScore();
        Customer customer=userMapper.detail(order.getCid());
        score.setCid(order.getCid());
        score.setMscore(1l);
        //score.setTscore(1l);
        score.setUpdatetime(System.currentTimeMillis());
        scoreMapper.increaseScore(score);
        //增加提成积分
        if(customer!=null&&customer.getVipuid()!=null) {
            CustomerScore toscore = new CustomerScore();
            toscore.setCid(customer.getVipuid());
            //toscore.setMscore(1l);
            toscore.setTscore(1l);
            score.setUpdatetime(System.currentTimeMillis());
            scoreMapper.increaseScore(toscore);
        }
        CustomerScoreLog log=new CustomerScoreLog();
        log.setCid(order.getCid());
        log.setMscore(1l);
        log.setTscore(1l);
        log.setSrctype(1);
        log.setOrderid(order.getOrderid());
        if(customer.getVipuid()!=null) {
            log.setTocid(customer.getVipuid());
            log.setCreatetime(System.currentTimeMillis());
            scoreMapper.addScoreLog(log);
            Customer tocustomer=userMapper.detail(customer.getVipuid());
            sendOrderMessage(tocustomer.getTruename(),customer.getTruename(),order.getProduct(),tocustomer.getMobile());
            if(order.getGiftid()!=null){
                CustomerScore vipscore=scoreMapper.scoreDetail(tocustomer.getCid());

                Gift gift=giftService.detail(order.getGiftid());
                if(vipscore.getTscore()<(gift.getScore()*order.getGiftnum()))
                    throw new ServiceException(DBExceptionEnums.SCORE_IS_LIMIT);
                log.setCid(tocustomer.getCid());
                log.setTscore(0-gift.getScore()*order.getGiftnum());
                log.setCid(System.currentTimeMillis());
                log.setSrctype(2);
                scoreMapper.addScoreLog(log);
                CustomerScore vscore=new CustomerScore();
                vscore.setCid(customer.getVipuid());
                vscore.setUpdatetime(System.currentTimeMillis());
                vscore.setTscore(0-gift.getScore()*order.getGiftnum());
                scoreMapper.increaseScore(vscore);



            }
        }
    }
    public void sendOrderMessage(String vipname,String customername,String product,String mobile){
        JSONObject obj=new JSONObject();
        obj.put("name",vipname);
        obj.put("customer",customername);
        obj.put("product",product);
        String params=obj.toJSONString();
        SnsMessageUtil.sendMessage("18311033087","SMS_138060784",params);

    }


}
